package com.heima.article.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.HotArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.common.schedule.CacheService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.vos.HotArticleVo;
import javafx.beans.binding.ListBinding;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class HotArticleServiceImpl implements HotArticleService {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Override
    public void computeHotArticle() {
        LocalDateTime dateTime = LocalDateTime.now().minusDays(5);
        List<ApArticle> list = apArticleMapper.findArticleByLast5days(dateTime);
        if (CollectionUtils.isEmpty(list)){
            log.info("文章内容为空");
            return;
        }
        List<HotArticleVo> hotArticleVos = hotArticleVos(list);
        if (CollectionUtils.isEmpty(hotArticleVos)){
            return;
        }
        List<List<HotArticleVo>> lists = CollectionUtil.groupByField(hotArticleVos,"channelId");
        if (CollectionUtils.isEmpty(lists)){
            return;
        }
        for (List<HotArticleVo> articleVos : lists) {
            HotArticleVo hotArticleVo = articleVos.get(0);
            Integer channelId = hotArticleVo.getChannelId();
            hotScore(articleVos,channelId.toString());
        }
       hotScore(hotArticleVos,ArticleConstants.DEFAULT_TAG);



    }
    //排序并存到redis
    private void  hotScore(List<HotArticleVo> vos,String channelId){
        if (CollectionUtils.isEmpty(vos)){
            return;
        }
        List<HotArticleVo> collect = vos.stream().sorted(new Comparator<HotArticleVo>() {
                @Override
                public int compare(HotArticleVo o1, HotArticleVo o2) {
                    Integer score = o1.getScore();
                    Integer score1 = o2.getScore();
                    return score - score1;
                }
            }).collect(Collectors.toList());
            if (collect.size()>30){
                collect.subList(0,30);
            }
            cacheService.set(ArticleConstants.HOT_ARTICLE_FIRST_PAGE+channelId, JSON.toJSONString(collect));


        }


    //计算集合中所有文章的总热度
    private List<HotArticleVo> hotArticleVos(List<ApArticle> list){
        if (CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        List<HotArticleVo> hotList = new ArrayList<>();
        Integer score=0;
        for (ApArticle apArticle : list) {
            if (apArticle==null){
                continue;
            }
            //阅读
            HotArticleVo hotArticleVo = new HotArticleVo();
            Integer views = apArticle.getViews();
            if (views!=null){
                score+=views;
            }
            //点赞
            Integer likes = apArticle.getLikes();
            if (likes!=null){
                score+=likes * 3;
            }
            //评论
            Integer comment = apArticle.getComment();
            if (comment!=null){
                score+=comment *5;
            }
            //收藏
            Integer collection = apArticle.getCollection();
            if (collection!=null){
                score+=collection *8;
            }
            BeanUtils.copyProperties(apArticle,hotArticleVo);
            hotArticleVo.setScore(score);
            hotList.add(hotArticleVo);
        }
        return hotList;
    }
}
